package com.hh.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.constant.CategoryConstance;
import com.hh.entity.Category;
import com.hh.mapper.CategoryMapper;
import com.hh.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品类目 服务实现类
 * </p>
 *
 * @author 辉辉
 * @since 2022-08-16
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "com.hh.service.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 全查询商品分类表
     * 需要缓存
     * 注解
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstance.CATEGORY_PREFIX)
    public List<Category> findList() {
        return categoryMapper.selectList(Wrappers.<Category>lambdaQuery().orderByDesc(Category::getUpdateTime));
    }

    /**
     * 查询所有父节点
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstance.CATEGORY_ROOT_PREFIX)
    public List<Category> findRootCategory() {
        return categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getParentId, 0L));
    }

    /**
     * 新增商品分类
     * 删除缓存 所有相关的缓存 要不然会出现脏读
     *
     * @param category
     * @return
     */
    @Override
    // 删除单个用这个
//    @CacheEvict(key = CategoryConstance.CATEGORY_ROOT_PREFIX)
    @Caching(evict = {
            @CacheEvict(key = CategoryConstance.CATEGORY_ROOT_PREFIX),
            @CacheEvict(key = CategoryConstance.CATEGORY_PREFIX)
    })
    public boolean save(Category category) {
        log.info("新增商品分类{}", JSON.toJSONString(category));
        category.setRecTime(new Date());
        category.setUpdateTime(new Date());
        category.setShopId(1L);
        // 新增父节点 还是子节点
        Long parentId = category.getParentId();
        if (ObjectUtils.isEmpty(parentId)) {
            // 新增父节点
            parentId = 0L;
            category.setGrade(1);
        } else {
            // 新增子节点
            // 校验一下
            Category parent = categoryMapper.selectOne(Wrappers.<Category>lambdaQuery()
                    .eq(Category::getParentId, parentId)
            );
            if (ObjectUtils.isEmpty(parent)) {
                // 父节点不存在
                throw new IllegalArgumentException("新增商品分类时父节点不存在");
            }
            category.setGrade(2);
        }
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 前台根据父节点查询分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findCategoryByParentId(Long parentId) {
        // 如果是空的话
        if (parentId == null || parentId == 0) {
            parentId = 0L;
        }
        // 查询父节点
        List<Category> categories = categoryMapper.selectList(Wrappers.<Category>lambdaQuery()
                .eq(Category::getParentId, parentId)
        );
        return categories;
    }
}
